<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/29
 * Time: 10:12
 */

namespace app\admin\model;
use think\Model;
use think\Db;
class FeedbackModel extends Model
{
    // 确定链接表名
    protected $table = 'snake_feedback';
    public function getFeedbackByWhere($where, $offset, $limit,$type=1)
    {
        return Db::name('feedback')->alias('f')->field('f.*,u.nickname,u.head_pic,u.mobile')
            ->join('__USERS__ u','f.user_id = u.user_id','LEFT')
            ->where($where)->order('id desc')->limit($offset, $limit)->select();
    }
    public function getAllFeedback($where,$type=1)
    {
        return $this->alias('f')->where($where)->count();
    }
    public function getOneFeedback($id)
    {
        return $this->where("id=$id")->find();
    }

    public function reply($param){
        $data=$this->getOneFeedback($param['id']);
        try{
            $result=$this->save(array('status'=>1,'replytime'=>time(),'reply'=>$param['reply']),['id'=>$param['id']]);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                $map=array('to_user'=>$data['user_id'],'title'=>'感谢您的反馈','message'=>$param['reply']);
                $model = new MsgModel();
                $flag = $model->add($map);
                return msg(1, url('feedback/index'),'回复成功');
            }
        }catch(\Exception $e){
            return msg(-1, '', $e->getMessage());
        }
    }
    public function replayFeedback($param,$type)
    {
        try{
            $param['status']=2;
            $param['replytime']=time();
            $result = $this->validate('FeedbackValidate')->save($param, ['id' => $param['id']]);

            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                if($type==2){
                    $page='complaint';

                    if(isset($param['user_status']) && ($param['user_status']==2 || $param['user_status']==3)){
                        $complain_user_id=$this->where(array('id'=>$param['id']))->value('complain_user_id');
                        $user = new UsersModel();
                        $map=array(
                            'id'=>$complain_user_id,
                            'user_id'=>$complain_user_id,
                            'status'=>$param['user_status'],
                            'status_time'=>time()
                        );
                        $flag = $user->editUser($map);
                    }

                }
                if($type==3){$page='report';}
                return msg(1, url($page.'/index'), '回复成功');
            }
        }catch(\Exception $e){
            return msg(-2, '', $e->getMessage());
        }
    }
}